package com.cheer.controller;

import com.cheer.dto.DocumentDTO;
import com.cheer.dto.StandingBookSaveDTO;
import com.cheer.dto.StandingBookSubmitDTO;
import com.cheer.security.UserInfoHolder;
import com.cheer.service.StandingBookService;
import com.cheer.vo.DocumentNumberVO;
import com.cheer.vo.Result;
import com.cheer.vo.StandingBookVO;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * @author cheer
 */
@RestController
@RequestMapping("/book")
public class StandingBookController {

    @Autowired
    private StandingBookService standingBookService;

    @Autowired
    private UserInfoHolder userInfoHolder;

    /**
     * 省级文明号申请
     */
    @GetMapping("/apply")
    @ApiOperation("省级文明号申请")
    public Result<Boolean> apply() {
        Boolean result = standingBookService.apply(userInfoHolder.getAccountId());
        return Result.success(result);
    }

    /**
     * 查看台账
     */
    @GetMapping("/info")
    @ApiOperation("查看台账")
    public Result<StandingBookVO> getStandingBook() {
        StandingBookVO standingBookVO = standingBookService.getByAccountId(userInfoHolder.getAccountId());
        return Result.success(standingBookVO);
    }

    /**
     * 保存台账信息
     */
    @PostMapping("/save")
    public Result saveStandingBook(@RequestBody @Valid StandingBookSaveDTO standingBookSaveDTO) {
        standingBookService.saveStandingBook(userInfoHolder.getAccountId(), standingBookSaveDTO);
        return Result.success();
    }

    /**
     * 查看台账资料
     */
    @GetMapping("/document")
    public Result<List<DocumentNumberVO>> getDocument() {
        List<DocumentNumberVO> list = standingBookService.getDocument(userInfoHolder.getAccountId());
        return Result.success(list);
    }

    /**
     * 保存台账资料
     */
    @PostMapping("/document/save")
    public Result saveDocument(@RequestBody List<DocumentDTO> documentDTOS) {
        standingBookService.saveDocument(userInfoHolder.getAccountId(), documentDTOS);
        return Result.success();
    }

    /**
     * 批量下载台账文档
     */
    @GetMapping("/document/download")
    public Result<String> download() {
        String zipUrl = standingBookService.downloadDocument(userInfoHolder.getAccountId());
        return Result.success(zipUrl);
    }

    /**
     * 提交台账
     */
    @PostMapping("/submit")
    public Result submitStandingBook(@RequestBody StandingBookSubmitDTO standingBookSubmitDTO) {
        standingBookService.submitStandingBook(userInfoHolder.getAccountId(), standingBookSubmitDTO);
        return Result.success();
    }


    @ApiOperation(value = "补丁文件", notes = "新添加状态将一号文件转变为 5 号文件")
    @PostMapping("/patch")
    public Result patch(){
        standingBookService.patch();
        return Result.success();
    }

}
